package enviornment;

import main.BuildModel;
/**
 * This class is the clock time of the simulation.
 * @author Chunhui Li
 *
 */
public class Clock {
	private int day;
	private int hour;
	private int minute;
	

	public Clock( ) {
		day = 0;
		hour= 0;
		minute = 0;
	}
	
	public Clock convertTicks(double tick){
		
		double time = tick/BuildModel.TICK_PER_HOUR;
		String s_time = String.valueOf(time);
		String[] s = s_time.split("\\.");
		int temp_hour =  Integer.parseInt(s[0]);
		hour = BuildModel.START_HOUR + temp_hour;
		setDay(hour/24);
		setHour(hour%24);
		minute = (int)Math.round((time-temp_hour)*60);
		
		return this;
	}
	/**
	 * Converts a duration time in ticks into working days.
	 * @param tick
	 * @return a time in clock time
	 */
	public Clock workingHours(double tick, int workhours_per_day){		
		Clock working_hours= new Clock();
		double time = tick/BuildModel.TICK_PER_HOUR;
	//	System.out.println(time);
		String s_time = String.valueOf(time);
		String[] s = s_time.split("\\.");
		int hour =  Integer.parseInt(s[0]);
		working_hours.setDay(hour/workhours_per_day);
		working_hours.setHour(hour% workhours_per_day);
		working_hours.setMin((int)Math.round((time-hour)*60));
		
		return working_hours;
	}
	/*
	public Clock substractClockTime(Clock c){
		int m=0; int h=0; int d=0;
		if(this.getMin()>=c.getMin()){
			m= this.getMin() - c.getMin();
		}
		if(this.getMin()<c.getMin()){
			if(this.getHour()==0){
				this.setDay(getDay()-1);
				this.setHour(24);
			}
			if(this.getHour()>0){
				this.setHour(getHour()-1);
				m=60+this.getMin()-c.getMin();
			}			
		}
		if(this.getHour()>=c.getHour()){
			h= this.getHour()- c.getHour();
		}
		 if(this.getHour)
		d = this.getDay() - c.getDay();
		
	}*/
	public double workingHoursBetweenTwo (Clock c1, Clock c2){
		double workinghours=0.0;
		if(c2.getDay()>c1.getDay()){
			int days=c2.getDay()-c1.getDay();
			double h= c1.getHour()+c1.getMin()/60.0;
			double x=(double)BuildModel.END_WORK_HOUR-h;
			days--;
			if(days>0){
				x=x+days*BuildModel.WORKINGHOURS;
			}
			workinghours=x+c2.getHour()-BuildModel.START_WORK_HOUR+c2.getMin()/60.0;
		}
		if(c2.getDay()==c1.getDay()){
			if(c2.getMin()<c1.getMin()){
				c2.setHour(c2.getHour()-1);
				c2.setMin(c2.getMin()+60);
			}
			int d_h=c2.getHour()-c1.getHour();
			int d_m=c2.getMin()-c1.getMin();
			workinghours=d_h+d_m/60.0;
		}
		return workinghours;
	}
	public Clock addWorkingTime(Clock c){
		String s = this.toString();
		int d = this.getDay() + c.getDay();
		int h= this.getHour()+c.getHour();
		int m= this.getMin() + c.getMin();
		if(m >= 60){
			m=m-60;
			h= h+1;
		}
		if(h<BuildModel.START_WORK_HOUR){
			h=h+BuildModel.START_WORK_HOUR;			
		}
		//previously, h can be 0, which causes problems
		if(h>=BuildModel.END_WORK_HOUR){//if(h >= 24){
			h=h-BuildModel.END_WORK_HOUR+BuildModel.START_WORK_HOUR;
			d= d+1;
		}
		
		this.setDay(d);
		this.setHour(h);
		this.setMin(m);
	//	System.out.println("^^^^^^^^^^^^^^ADDING WORKIGN HOURS: "+s +" + "+ c.toString()+" = "+ this.toString() );
		return this;
	}
	//c1-c2
	public Clock subtractClockTime(Clock c1, Clock c2){
		Clock result = new Clock();
		int d1 =c1.getDay();
		int h1 = c1.getHour();
		int m1 = c1.getMin();
		int d2 =c2.getDay();
		int h2 = c2.getHour();
		int m2 = c2.getMin();
		double substraction =  d1*24+h1+m1/60 -d2*24+h2+m2/60 ;
		String s_time = String.valueOf(substraction);
		String[] s = s_time.split("\\.");
		int hour =  Integer.parseInt(s[0]);
		result.setDay(hour/24);
		result.setHour(hour%24);
		result.setMin((int)Math.round((substraction-hour)*60));
		
		return result;
	}
	
	public double nextDay(){
		int h=this.getHour();
		if(h<BuildModel.START_WORK_HOUR){
			this.setHour(BuildModel.START_WORK_HOUR);
		}
		else{//if(h>=BuildModel.END_WORK_HOUR){
			this.setHour(BuildModel.START_WORK_HOUR);
			this.setDay(this.getDay()+1);
		}
		double tick=this.convertClock();
		return tick;
	}
	public double SubtractionToWorkingHours(Clock c1, Clock c2){
		
		double workingHours = 0.0;
		int d1 =c1.getDay();
		int h1 = c1.getHour();
		int m1 = c1.getMin();
		int d2 =c2.getDay();
		int h2 = c2.getHour();
		int m2 = c2.getMin();
		if(d1==d2){
			if (h1>h2){			
				workingHours = h1-1-h2+(m1+60-m2)/60.0;				
			}
			if(h1==h2){
				workingHours = (m1-m2)/60.0;
			}
		}
		if(d1>d2){
			double temp1= h1-BuildModel.START_WORK_HOUR+m1/60.0;			
			double temp2= BuildModel.END_WORK_HOUR-1-h2 +(60-m2)/60.0;
			if(h2>BuildModel.END_WORK_HOUR){
				System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Error in finishing working hour");
				temp2 = h2-1- BuildModel.END_WORK_HOUR +(60-m2)/60.0;
			}
			workingHours = (d1-d2-1)*BuildModel.WORKINGHOURS + temp1+ temp2;
		}
		
		return workingHours;
	}
	
	public double convertClock(Clock c){
		double ticks= (c.getDay()*24.0 + c.getHour() +c.getMin()/60.0)* BuildModel.TICK_PER_HOUR;
		return ticks;
	}
	/**
	 * Covert clock time to ticks in the simulation
	 * @return
	 */
	public double convertClock(){
		double ticks= (this.getDay()*24.0 + this.getHour()-BuildModel.START_HOUR +this.getMin()/60.0)* BuildModel.TICK_PER_HOUR;
		return ticks;
	}

	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}
	
	public int getHour() {
		return hour;
	}

	public void setHour(int h) {
		this.hour = h;
	}
	public int getMin() {
		return minute;
	}

	public void setMin(int min) {
		this.minute = min;
	}
	public String toString(){
		String s = this.getDay()+ ":"+ this.getHour()+ ":"+ this.getMin();
		return s;		
	}

	
}
